package com.lsf.dunlian.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.lsf.dunlian.Myappliction;
import com.lsf.dunlian.R;
import com.lsf.dunlian.entity.AllNoticeRecordData;
import com.lsf.dunlian.entity.FileEntity;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FileRecyclerViewAdapter extends RecyclerView.Adapter<FileRecyclerViewAdapter.ViewHolder> {
    private List<FileEntity> mDataList;
    private Context context;

    public FileRecyclerViewAdapter(List<FileEntity> mDataList, Context context) {
        this.mDataList = mDataList;
        this.context = context;
    }

    @NonNull
    @Override
    public FileRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_file, parent, false);
        FileRecyclerViewAdapter.ViewHolder holder = new FileRecyclerViewAdapter.ViewHolder(view);
        return holder;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        FileEntity entity = mDataList.get(position);
        holder.name.setText(entity.name);

        if (StringUtils.isNotEmpty(entity.src)){
            holder.my_progress.setVisibility(View.GONE);
            holder.jindu.setText("上传完成");
            holder.jindu.setTextColor(context.getColor(R.color.lan));
        }else {
            holder.my_progress.setProgress(Math.toIntExact(entity.progress));
            holder.my_progress.setMax(Math.toIntExact(entity.sum));
        }
        holder.del.setOnClickListener(v -> {
            Myappliction.fileEntityList.remove(entity);
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.my_progress)
        ProgressBar my_progress;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.jindu)
        TextView jindu;
        @BindView(R.id.del)
        LinearLayout del;
//        @BindView(R.id.name)
//        TextView name;
//        @BindView(R.id.time)
//        TextView time;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
